<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>登录新闻聚合平台</h2>
        <p>随时掌握最新热点资讯</p>
      </div>
      
      <el-form 
        :model="form" 
        :rules="rules" 
        ref="loginForm" 
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="用户名"
            prefix-icon="User"
            size="large"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="密码"
            prefix-icon="Lock"
            size="large"
            type="password"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            @click="submitForm"
            :loading="loading"
            class="login-btn"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>还没有账号？<router-link to="/register">立即注册</router-link></p>
        <p><router-link to="/">返回首页</router-link></p>
      </div>
    </div>
    
    <div class="login-banner">
      <div class="banner-content">
        <h3>实时热点 · 一手掌握</h3>
        <p>登录后收藏您感兴趣的新闻内容，随时随地查看最新资讯</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { ElMessage } from 'element-plus';

const router = useRouter();
const userStore = useUserStore();

const form = ref({
  username: '',
  password: ''
});

const loading = ref(false);
const loginForm = ref(null);

const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
});

const submitForm = () => {
  loginForm.value.validate(valid => {
    if (!valid) return;
    
    loading.value = true;
    
    userStore.login(form.value)
      .then(() => {
        ElMessage.success('登录成功');
        router.push('/');
      })
      .catch(error => {
        ElMessage.error(error.message || '登录失败');
      })
      .finally(() => {
        loading.value = false;
      });
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  background: #f5f7fa;
}

.login-card {
  width: 450px;
  background: #fff;
  padding: 50px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
  z-index: 1;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 16px;
  color: #666;
}

.login-form {
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  margin-top: 10px;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  color: #666;
  font-size: 14px;
}

.login-footer a {
  color: #409EFF;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}

.login-banner {
  flex: 1;
  background: linear-gradient(135deg, #409EFF 0%, #64b5ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.banner-content {
  max-width: 600px;
  padding: 30px;
  color: white;
  text-align: center;
  z-index: 1;
}

.banner-content h3 {
  font-size: 36px;
  margin-bottom: 20px;
  font-weight: 600;
}

.banner-content p {
  font-size: 18px;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .login-banner {
    display: none;
  }
  
  .login-card {
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
  }
}

@media (max-width: 576px) {
  .login-card {
    padding: 30px 20px;
  }
}
</style>